﻿var SearchBar = function(container, hasSettings, filter) {
    var self = this;
    var lastSearchText = "";

    var resetWasVisible = false;

    var Elements = {
        searchInput: container.find('.input-search'),
        searchInputReset: container.find('.search-input-reset'),
        searchButton: container.find('.btn-search'),
        settingsButton: container.find('.btn-settings'),
        resetButton: container.find('.btn-reset')
    };

    this.getSearchText = function() {
        return lastSearchText;
    };

    this.getCurrentText = function() {
        return Elements.searchInput.val();
    };

    this.initialize = function() {
        container.parents('.content').css('margin-top', '40px');

        if (hasSettings) {
            Elements.settingsButton.show();

            Elements.settingsButton.tap(function() {
                Event.sendEvent(container, 'settings');
            });
        }

        Elements.searchInput.keydown(function(e) {
            if (Elements.searchInput.val().length > 0) {
                Elements.searchInputReset.show();
            } else Elements.searchInputReset.hide();

            if (e.which == 13 || e.which == 10) {
                startSearch();
                return false;
            }
        });

        Elements.searchInputReset.tap(function() {
            Elements.searchInput.val("");
            $(this).hide();
            lastSearchText = "";
        });

        Elements.searchButton.click(function() {
            startSearch();
        });

        Elements.resetButton.tap(function() {
            self.reset();
        });

        Elements.searchInput.bind('focusin focusout', function() {
            setTimeout(function() {
                $(this).parents('div.search-bar').css('top', '0px');
            }, 100);
        });
    };

    var startSearch = function() {
        var searchText = Elements.searchInput.val();

        lastSearchText = searchText;

        if (filter && !filter.hasAnyCriteriaSelected())
            Elements.resetButton.hide();

        if (Elements.resetButton.is(":visible"))
            resetWasVisible = true;
        else resetWasVisible = false;

        if (Elements.searchInput.val() || (filter && filter.hasAnyCriteriaSelected()))
            Elements.resetButton.show();

        if (filter)
            filter.updateFilterExplanation();

        Event.sendEvent(container, 'getData', {
            reset: true,
            searchText: searchText || ""
        });
    };

    this.search = function() {
        startSearch();
    };

    this.setSearchData = function(searchText, apply) {
        Elements.searchInput.val(searchText);
        if (apply)
            self.startSearch();
    };

    this.hideSearch = function() {
        if (!resetWasVisible)
            Elements.resetButton.hide();
    };

    this.show = function() {
        Elements.searchInput.show();
    };

    this.reset = function(doNotSendEvent) {
        Elements.searchInput.val("");
        $(this).hide();
        Elements.searchInputReset.hide();
        lastSearchText = "";
        Elements.resetButton.hide();
        if (!doNotSendEvent)
            Event.sendEvent(container, 'cancel');
    };

    this.setPlaceholderText = function(text) {
        Elements.searchInput.attr('placeholder', text);
    };
};